package dcc.component;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellEditor;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import dcc.mbg.model.User;
import dcc.tools.BeanFactory;

public class JUserTable extends JTable {

	private JUserManagePanel userManagePanel;

	public JUserTable(JUserManagePanel userManagePanel) {
		this.userManagePanel = userManagePanel;
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		this.setRowHeight(30);
		this.getTableHeader().setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (1 == e.getClickCount()) {
					getSelectedUser();
				}
			}
		});
	}

	private void getSelectedUser() {
		int rowIndex = this.getSelectedRow();
		if (-1 == rowIndex) {
			return;
		} else {
			User user = new User();
			user.setUserName(this.getModel().getValueAt(rowIndex, 0).toString());
			user.setPassword(this.getModel().getValueAt(rowIndex, 1).toString());
			user.setName(this.getModel().getValueAt(rowIndex, 2).toString());
			user.setRole(this.getModel().getValueAt(rowIndex, 3).toString());
			userManagePanel.showSelectedRow(user);
		}
	}

	protected void initDataBindings(List<User> users) {
		JTableBinding<User, List<User>, JTable> jTableBinding = SwingBindings
				.createJTableBinding(UpdateStrategy.READ, users, this,
						"bindingRole");
		BeanProperty<User, String> userName = BeanProperty.create("userName");
		jTableBinding.addColumnBinding(userName)
				.setColumnName("\u7528\u6237\u540D").setEditable(false);
		BeanProperty<User, String> password = BeanProperty.create("password");
		jTableBinding.addColumnBinding(password).setColumnName("\u5BC6\u7801")
				.setEditable(false);
		BeanProperty<User, String> name = BeanProperty.create("name");
		jTableBinding.addColumnBinding(name)
				.setColumnName("\u771F\u5B9E\u59D3\u540D").setEditable(false);
		BeanProperty<User, String> role = BeanProperty.create("role");
		jTableBinding.addColumnBinding(role).setColumnName("\u89D2\u8272")
				.setEditable(false);

		//
		BeanProperty<User, String> deleteButtonBeanProperty = BeanProperty
				.create("userName");
		jTableBinding.addColumnBinding(deleteButtonBeanProperty).setColumnName(
				"\u64CD\u4F5C");
		jTableBinding.bind();

		this.getColumnModel().getColumn(4).setMaxWidth(40);
		getColumnModel().getColumn(4).setCellRenderer(
				new DeleteButtonRenderer());
		getColumnModel().getColumn(4).setCellEditor(
				new UserDeleteButtonEditor(this));
	}

	public void update() {
		// List<User> users = ((UserService) BeanFactory.getBean("userService"))
		// .getAllUser();
		// initDataBindings(users);
		this.getColumnModel().getColumn(1).setMinWidth(0);
		this.getColumnModel().getColumn(1).setMaxWidth(0);
	}

	private class UserDeleteButtonEditor extends AbstractCellEditor implements
			TableCellEditor, ActionListener {

		private JButton button;
		private JUserTable userTable;

		public UserDeleteButtonEditor(JUserTable userTable) {
			this.userTable = userTable;
			button = new JButton("\u5220\u9664");
			button.addActionListener(this);
		}

		@Override
		public Object getCellEditorValue() {
			return button.getActionCommand();
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// ((UserService) BeanFactory.getBean("userService"))
			// .removeUserByUserName(button.getActionCommand());
			fireEditingStopped();
			userTable.update();
		}

		@Override
		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			this.button.setActionCommand("" + value);
			return button;
		}

	}

}
